// pages/order/order.ts
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';
import { getAllOrder, cancelOrders, deleteOrders, orderLeaveWord } from "../../utils/api";
let type = 0;
let shopId = "";
let hairstylistId = "";
let message = ""
Page({
  data: {
    isRef: true,
    contentHeight: 0,
    titleArrList: [
      {
        id: 0,
        value: "全部",
        isActive: true,
        target: "all"
      },
      {
        id: 1,
        value: "未完成",
        isActive: false,
        target: "undone"
      },
      {
        id: 2,
        value: "已完成",
        isActive: false,
        target: "done"
      },
      {
        id: 3,
        value: "已取消",
        isActive: false,
        target: "cancel"
      }
    ],
    renderArr: [],
    commentState: false,
    shopScoring: 5,
    hairstylistScoring: 5
  },
  onLoad() {
    type = 0;
    this.onGetAllOrder();
  },
  onOrderLeaveWord() {
    if (!message) {
      Toast.fail("评论为空");
      return;
    };
    orderLeaveWord({
      shopId,
      hairstylistId,
      shopNumber: this.data.shopScoring,
      hairstylistNumber: this.data.hairstylistScoring,
      leaveWordContent: message
    }).then(res=>{
      if(res){
        Toast.success("发表成功");
      }
    })
  },
  onMessageInput(e: any) {
    message = e['detail'].value;
    console.log(e['detail']);
  },
  onShopScoring(event: any) {
    this.setData({
      shopScoring: event.detail,
    });
  },
  onHairstylistScoring(event: any) {
    this.setData({
      hairstylistScoring: event.detail,
    });
  },
  // 设置评论
  onCommentMessage() {

  },
  onCommentShow(e: any) {
    console.log(e.currentTarget.dataset);
    shopId = e.currentTarget.dataset.shopid;
    hairstylistId = e.currentTarget.dataset.hairstylist;
    this.setData({
      commentState: true
    })
  },
  onCommentHide() {
    this.setData({
      commentState: false
    })
  },
  breakPage(e: any) {
    wx.navigateTo({
      url: `../orderDetails/orderDetails?id=${e.currentTarget.dataset.id}`
    })
  },
  onClick(event: any) {
    const target = event.currentTarget.dataset.target;
    const TEMPVAR = this.data.titleArrList;
    TEMPVAR.forEach(item => {
      if (item.target === target) {
        if (item['target'] == "all") {
          type = 0
        }
        if (item['target'] == "undone") {
          type = 1
        }
        if (item['target'] == "done") {
          type = 2
        }
        if (item['target'] == "cancel") {
          type = 3
        }
        item.isActive = true
      } else {
        item.isActive = false
      }
    })
    this.setData({
      titleArrList: TEMPVAR
    })
    this.onGetAllOrder()
  },
  // 获取时分
  getHM(ms: number) {
    let h = Math.floor(ms / 1000 / 60 / 60) > 9 ? Math.floor(ms / 1000 / 60 / 60) : '0' + Math.floor(ms / 1000 / 60 / 60);
    let m = Math.floor(ms / 1000 / 60 % 60) > 9 ? Math.floor(ms / 1000 / 60 % 60) : '0' + Math.floor(ms / 1000 / 60 % 60);
    return `${h}小时${m}分`;
  },
  // 获取服务器数据
  async onGetAllOrder() {
    const { data } = await getAllOrder({
      page: 1,
      number: 20,
      type
    })
    data.rows.forEach((ele: any) => {
      if (ele['appointmentTime']) {
        ele['appointmentTime'] = ((+ele['appointmentTime']) - +new Date());
      }
    });
    this.setData({
      renderArr: data.rows,
    }, () => {
      this.setData({
        isRef: false
      })
    })
  },
  onShow() {
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 3,
        show: true
      })
    }
    const BC = wx.getMenuButtonBoundingClientRect();
    const WC = wx.getWindowInfo();
    this.setData({
      contentHeight: WC.screenHeight - BC.top - BC.height - 85
    })
  },
  async cancelOrder(e: any) {
    Dialog.confirm({
      title: '操作确认',
      message: '是否取消该订单',
    })
      .then(async () => {
        const res = await cancelOrders({
          orderId: e.currentTarget.dataset.id
        })
        Toast.success('取消成功');
        this.onGetAllOrder();
      })
      .catch(() => {
        // on cancel
      });
  },
  onDeleteOrders(e: any) {
    Dialog.confirm({
      title: '操作确认',
      message: '是否删除该订单',
    })
      .then(async () => {
        const res = await deleteOrders({
          orderId: e.currentTarget.dataset.id
        })

        Toast.success('删除成功');
        this.onGetAllOrder();
      })
      .catch(() => {
        // on cancel
      });
  },
  onRefresh() {
    this.setData({
      isRef: true
    })
    this.onGetAllOrder();
  }
})